package com.gzist.blog_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzist.blog_system.domain.Article;
import com.gzist.blog_system.domain.Statistic;
import com.gzist.blog_system.mapper.StatisticMapper;
import com.gzist.blog_system.service.StatisticService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class StatisticServiceImpl extends ServiceImpl<StatisticMapper, Statistic>
        implements StatisticService {

    @Resource
    private StatisticMapper statisticMapper;

    @Override
    public boolean add(Article article) {
        Statistic statistic = new Statistic();
        statistic.setArticleId(article.getId());
        statistic.setHit(0L);
        statistic.setCommentNum(0L);
        int i = statisticMapper.insert(statistic);
        return i == 1;
    }

    @Override
    public Statistic selectOneByArticleId(Long articleId) {
        return statisticMapper.selectOne(new QueryWrapper<Statistic>().eq("article_id", articleId));
    }

    @Override
    public boolean updateHits(Statistic statistic) {
        return statisticMapper.updateHits(statistic);
    }

    @Override
    public boolean updateComments(Statistic statistic) {
        return statisticMapper.updateComments(statistic);
    }

    @Override
    public boolean deleteStatistic(Long id) {
        QueryWrapper<Statistic> article_id = new QueryWrapper<Statistic>().eq("article_id", id);
        int i = statisticMapper.delete(article_id);
        return i>=1;
    }

    @Override
    public List<Statistic> getStatistic() {
        return statisticMapper.getStatistic();
    }

    @Override
    public Long getTotalVisit() {
        return statisticMapper.getTotalVisit();
    }

    @Override
    public Long getTotalComment() {
        return statisticMapper.getTotalComment();
    }
}




